package com.ce.sys.account.action;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.SearchAction;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.sys.msg.action.SysMsgDefineAction;
import com.ce.util.DbUtil;
import com.ce.util.RegeditUtil;

/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：andy
* 创建日期：2013-8-3
* 创建时间：上午9:00:52
* 功能描述：实现个人用户的参数配置功能
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class UserConfAction extends SearchAction {
	private static final long serialVersionUID = 1L;
	private SysMsgDefineAction msgdefine = new SysMsgDefineAction();
	public static String DEFAULT_PAGE = "DEFAULT_PAGE";

	@SuppressWarnings("deprecation")
	public String execute() throws Exception {
		msgdefine.openSetSelfMsgPage();//个人消息配置
		User user = User.getUser();
		String url = user.getRegeditValue(DEFAULT_PAGE);
		String menuId = DbUtil.getFirstStringSqlQuery("select vc_id from sys_t_menu where vc_url=?",new Object[]{url});
		setAttribute("defaultPage", menuId);
		String rows = user.getRegeditValue(WebConstants.ROWS_PER_PAGE);
		if(StringUtil.isBlank(rows)){
			rows = String.valueOf(WebConstants.SEARCH_PAGE_SIZE);
		}
		StringBuffer buff = new StringBuffer();
		buff .append("<select name=\"rows\" style='width:120px;'>");
		String pages = RegeditUtil.getRegeditStringValue(RegeditUtil.SYSTEM_PAGE_LIST);
		if(StringUtil.isBlank(pages)){
			pages = "10,20,50,100";
		}
		for(String p:pages.split(",")){
			if(StringUtil.isInteger(p)){
				buff.append("<option value='").append(p).append("'").append(p.equals(rows)?" selected":"")
				.append(">每页显示").append(p).append("条</option>");
			}
		}
		buff.append("</select>");
		setAttribute("rowsPerPage", buff.toString());
		return SUCCESS;
	}

	public String save() throws AppException{
		try{
			User user = User.getUser();
			String menuId = getParameter("defaultPage");
			String url = DbUtil.getFirstStringSqlQuery("select vc_url from sys_t_menu where vc_id=?",new Object[]{menuId});
			String text = DbUtil.getFirstStringSqlQuery("select vc_name from sys_t_menu where vc_id=?",new Object[]{menuId});
			if(DbUtil.getFirstIntSqlQuery("select count(*) from sys_t_regedit_user where vc_user=? and vc_name=?",
					new Object[]{user.getVcId(),DEFAULT_PAGE})==0){
				DbUtil.executeSqlUpdate("insert into sys_t_regedit_user(vc_id,vc_user,vc_item,vc_name,vc_del$flag) values(sys_guid(),?,?,?,0)",
						new Object[]{user.getVcId(),"登录后默认显示页面",DEFAULT_PAGE});
				DbUtil.executeSqlUpdate("insert into sys_t_regedit_user(vc_id,vc_user,vc_item,vc_name,vc_del$flag) values(sys_guid(),?,?,?,0)",
						new Object[]{user.getVcId(),"登录后默认显示页面标题",DEFAULT_PAGE+"_TITLE"});
			}
			DbUtil.executeSqlUpdate("update sys_t_regedit_user set vc_value=? where vc_name=?",
					new Object[]{url,DEFAULT_PAGE});
			DbUtil.executeSqlUpdate("update sys_t_regedit_user set vc_value=? where vc_name=?",
					new Object[]{text,DEFAULT_PAGE+"_TITLE"});
			getResponse().setBufferSize(1024);
			msgdefine.setSelfMsgPage();//保存个人消息定制信息
			getResponse().reset();//清除上一个方法中写入response中的内容
			String rows = this.getParameter("rows");
			if(DbUtil.getFirstIntSqlQuery("select count(*) from sys_t_regedit_user where vc_user=? and vc_name=?",
					new Object[]{user.getVcId(),WebConstants.ROWS_PER_PAGE})==0){
				DbUtil.executeSqlUpdate("insert into sys_t_regedit_user(vc_id,vc_user,vc_item,vc_name,vc_del$flag) values(sys_guid(),?,?,?,0)",
						new Object[]{user.getVcId(),"默认每页显示行数",WebConstants.ROWS_PER_PAGE});
			}
			DbUtil.executeSqlUpdate("update sys_t_regedit_user set vc_value=? where vc_name=?",
					new Object[]{rows,WebConstants.ROWS_PER_PAGE});
			user.clearRegData();
			getRequest().getSession().setAttribute(WebConstants.SEARCH_PAGE_SIZE_FLDNAME, user.getRegeditValue(WebConstants.ROWS_PER_PAGE));
		}catch(Exception e){
			return super.returnInfoForJS(false, "保存失败！");
		}
		return super.returnInfoForJS(true,"保存成功！");
	}
	
}
